import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;


/**
 * Allows users to login vi	a their UserID, password,
 * and position
 * @author Henry Yuen
 *
 */
public class LoginFrame extends JFrame implements ActionListener {

	private JTextField userNameField;
	private JPasswordField passwordField;
	private JComboBox positionCombo;

	private JButton loginButton;
	private JButton cancelButton;

	private static String[] positionList =
		{"Supervisor", "Traffic Manager", "Flight Data/Clearance Delivery",
			"Ground Control", "Local Control", "Helicopter Control"
		};

	/**
	 * Constructor for LoginFrame
	 * initializes the UI components
	 */
	public LoginFrame() {

		//set the default close operation
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		//initialize the GUI elements
		initUI();
		setSize(350,200);
		setTitle("TowerControl Login");
		setVisible(true);

	}

	/**
	 * Instantiates all the UI elements and adds them
	 * to the content pane
	 */
	public void initUI() {
		userNameField = new JTextField();
		passwordField = new JPasswordField();

		//initialize the position combo
		positionCombo = new JComboBox(positionList);

		loginButton = new JButton("Login");
		cancelButton = new JButton("Cancel");

		loginButton.addActionListener(this);
		cancelButton.addActionListener(this);

		//set the properties of the JFrame itself, including
		//layout
		Container contentPane = getContentPane();
		setLayout(new GridBagLayout());

		GridBagConstraints cons = new GridBagConstraints();

		cons.fill = GridBagConstraints.HORIZONTAL;

		cons.insets = new Insets(5,5,5,5);
		cons.gridx = 0;
		cons.gridy = 0;
		contentPane.add(new JLabel("User Name"),cons);

		cons.insets = new Insets(5,0,5,0);
		cons.gridx = 1;
		cons.gridy = 0;
		contentPane.add(userNameField,cons);

		cons.insets = new Insets(5,5,5,5);
		cons.gridx = 0;
		cons.gridy = 1;
		contentPane.add(new JLabel("Password"),cons);

		cons.insets = new Insets(5,0,5,0);
		cons.gridx = 1;
		cons.gridy = 1;
		contentPane.add(passwordField,cons);

		cons.insets = new Insets(5,5,5,5);
		cons.gridx = 0;
		cons.gridy = 2;
		contentPane.add(new JLabel("Position"),cons);

		cons.insets = new Insets(5,0,5,0);
		cons.gridx = 1;
		cons.gridy = 2;
		contentPane.add(positionCombo,cons);

		cons.gridx = 0;
		cons.gridy = 3;
		cons.gridwidth = 2;
		cons.insets = new Insets(5,5,5,5);


		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridBagLayout());
		contentPane.add(buttonPanel,cons);

		cons = new GridBagConstraints();
		cons.fill = GridBagConstraints.HORIZONTAL;

		cons.gridx = 0;
		cons.gridy = 0;
		cons.insets = new Insets(5,10,5,10);
		buttonPanel.add(loginButton,cons);

		cons.gridx = 1;
		cons.gridy = 0;
		cons.weightx = 0;
		cons.insets = new Insets(5,10,5,10);
		buttonPanel.add(cancelButton,cons);
	}

	/**
	 * Responds to the actionPerformed
	 * @param e This contains the action that was performed
	 */
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == cancelButton) {
			//dispose
			dispose();
		} else if (e.getSource() == loginButton){

			String userName = userNameField.getText();
			String password = new String(passwordField.getPassword());

			//form validation
			if ("".equals(userName)) {
				JOptionPane.showMessageDialog(this,"You must type in a username");
				return;
			}

			String position = (String)positionCombo.getSelectedItem();

			SessionManager sessionManager =
					SessionManager.getInstance();

			String result = sessionManager.login(userName,password,position);

			if ("Success".equals(result)) {
				dispose();
				return;
			} else {
				JOptionPane.showMessageDialog(this,result);
				return;
			}




		}
	}






}
